package com.cy.module;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.ActivityContent;
import com.cy.model.ActivityQRCode;
import com.cy.model.ActivityUser;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

@ControllerBind(controllerKey = "/clmp/activityContent")
public class ActivityContentController extends WebApiController {

	@Override
	public void index() {
		List<ActivityContent> activityContents = ActivityContent.dao.getRootItems();
		 JSONArray jsonArray = new JSONArray();
		 for(ActivityContent activityContent : activityContents){
			 JSONObject jsonObject = new JSONObject();
			 jsonObject.put("activity_id", activityContent.getActivityId());
			 jsonObject.put("activity_name", activityContent.getActivityName());
			 jsonObject.put("parent_id", activityContent.getParentId());
			 jsonObject.put("activity_address", activityContent.getActivityAddress());
			 jsonObject.put("activity_date_start", activityContent.getActivityDateStart());
			 jsonObject.put("activity_date_end", activityContent.getActivityDateEnd());
			 jsonObject.put("activity_code", activityContent.getActivityCode());
			 jsonObject.put("status", activityContent.getStatus());
			 jsonObject.put("remark", activityContent.getRemark());
			 
			 List<ActivityContent> subs = activityContent.getChildren();
			 if(subs.size() > 0){
				 JSONArray subArray = new JSONArray();
				 for(ActivityContent sub : subs){
					 JSONObject subObj = new JSONObject();
					 subObj.put("activity_id", sub.getActivityId());
					 subObj.put("activity_name", sub.getActivityName());
					 subObj.put("parent_id", sub.getParentId());
					 subObj.put("activity_address", sub.getActivityAddress());
					 subObj.put("activity_date_start", sub.getActivityDateStart());
					 subObj.put("activity_date_end", sub.getActivityDateEnd());
					 subObj.put("activity_code", sub.getActivityCode());
					 subObj.put("status", sub.getStatus());
					 subObj.put("remark", sub.getRemark());
					 subArray.add(subObj);
				 }
				 jsonObject.put("children", subArray);
			 }
			 jsonArray.add(jsonObject);
		 }
		 renderJson(jsonArray.toJSONString());
	}

	@Override
	public void update() {
		ActivityContent tempActivityContent = getModel(ActivityContent.class);
		ActivityContent activityContent = ActivityContent.dao.findById(getParaToInt());
		if(tempActivityContent.getParentId() != null && !"".equals(tempActivityContent.getParentId()) && tempActivityContent.getParentId() != 0){
			activityContent.cover(tempActivityContent);
		} else {
			activityContent.setActivityName(tempActivityContent.getActivityName()).setStatus(tempActivityContent.getStatus()).setRemark(tempActivityContent.getRemark());
		}
		
		activityContent.setUpdateTime(new Date()).setUpdateUser(Integer.parseInt(String.valueOf(getLoginAccount().getAccountId())));
		if(activityContent.update()){
			renderSuccess();
		} else {
			renderFail();
		}
	}

	@Override
	public void save() {
		ActivityContent activityContent = getModel(ActivityContent.class);
		if(activityContent.getParentId() == null || "".equals(activityContent.getParentId())){
			activityContent.setParentId(0);
		}
		activityContent.setCreateTime(new Date()).setCreateUser(Integer.parseInt(String.valueOf(getLoginAccount().getAccountId())))
		.setUpdateTime(new Date()).setUpdateUser(Integer.parseInt(String.valueOf(getLoginAccount().getAccountId())));
		
		//保存二维码
		String[] codes = getParaValues("QRCode[]");
        if (codes != null){
            for(String code : codes){
                new ActivityQRCode().setActivityCode(activityContent.getActivityCode()).setQrCode(code).setCreateTime(new Date()).setCreateUser(Integer.parseInt(String.valueOf(getLoginAccount().getAccountId())))
                        .setUpdateTime(new Date()).setUpdateUser(Integer.parseInt(String.valueOf(getLoginAccount().getAccountId()))).save();
            }
        }

		if(activityContent.save()){
			renderSuccess();
		} else {
			renderFail();
		}
	}

	@Override
	public void delete() {
		try {
			ActivityContent.dao.delete(getParaToInt());
			renderSuccess();
		} catch (Exception e) {
			renderFail(e.getMessage());
		}
	}
	
	public void getParentItem(){
		List<ActivityContent> parentItems = ActivityContent.dao.getRootItems();
		JSONArray jsonArray = new JSONArray();
		
		for(ActivityContent activityContent : parentItems){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", activityContent.getActivityId());
			jsonObject.put("item_name", activityContent.getActivityName());
			jsonArray.add(jsonObject);
		}
		renderJson(jsonArray.toJSONString());
	}
	
	/**
	 * 
	 * @Title: ActivityContentController
	 * @Description: 保存扫描二维码的记录, 成功后返回二维码真实跳转地址
	 * @param: 
	 * @return: void
	 * @throws
	 */
	public void QRCodeHub(){
		String urlStr = StringUtils.trim(getPara("url"));
		Integer code = Integer.parseInt(StringUtils.trim(urlStr.substring(urlStr.length() - 6, urlStr.length())));
		String visitUrl = urlStr.substring(0, urlStr.length() - 6);
		
		ActivityUser activityUser = new ActivityUser();
		activityUser.setVisitUrl(visitUrl).setActivityCode(code).setCreateTime(new Date()).setCreateUser(0).setUpdateTime(new Date()).setUpdateUser(0);
		
		if(activityUser.save()){
			redirect(visitUrl);
		}
	}
	
	/**
	 * 
	 * @Title: ActivityContentController
	 * @Description: 查看二维码
	 * @param: 
	 * @return: void
	 * @throws
	 */
	public void getQRCode(){
		List<ActivityQRCode> qrCodes = ActivityQRCode.dao.findQRCode(Integer.parseInt(getPara("activityCode")));
		JSONArray jsonArray = new JSONArray();
		
		for(ActivityQRCode activityQRCode : qrCodes){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("qr_code", activityQRCode.getQrCode());
			jsonArray.add(jsonObject);
		}
		renderJson(jsonArray.toJSONString());
	}
}
